package com.chengzhi.web.service.impl;

import com.chengzhi.web.entity.DocBean;
import com.chengzhi.web.mapper.EsRepository;
import com.chengzhi.web.service.EsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;

/**
 * @description es-serviceimpl
 * @author: ruanchengzhi
 * @create: 2020-12-24 11:04
 **/
@Service
@Slf4j
public class EsServiceImpl implements EsService {

    @Autowired
    private EsRepository esRepository;

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    private Pageable pageable = PageRequest.of(0, 10);

    @Override
    public void createIndex() {
        restTemplate.createIndex(DocBean.class);
        log.info("[op:esServiceImpl] createIndex ");
    }

    @Override
    public void saveAll(List<DocBean> list) {
        esRepository.saveAll(list);
    }

    @Override
    public List<DocBean> selectAll() {
        List<DocBean> docBeanList = new ArrayList<>();
        Iterable<DocBean> all = esRepository.findAll();
        Iterator<DocBean> iterator = all.iterator();
        while (iterator.hasNext()) {
            docBeanList.add(iterator.next());
        }
        return docBeanList;
    }
}
